package com.sxyc.weixin.controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.api.SnsAccessToken;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;

public class IndexController extends Controller {

	public void index(){
		String code = getPara("code");
		String openId = (String) getRequest().getSession().getAttribute("openId");
		if(openId == null && code == null){
			String url = SnsAccessTokenApi.getAuthorizeURL(PropKit.get("appId"), PropKit.get("domain"), true);
			redirect(url);
		}else{
			if(openId == null){
				SnsAccessToken accessToken = SnsAccessTokenApi.getSnsAccessToken(PropKit.get("appId"), PropKit.get("appSecret"),code);
				openId = accessToken.getOpenid();
				getRequest().getSession().setAttribute("openId", openId);
				System.out.println("openid :"+openId);
			}
			renderJson("openid :"+openId);
		}
	}
}
